﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CreatePrimaryKey.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The create primary key.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.Common.Scripting
{
    using DbDiff.Common.DataAccess;

    /// <summary>
    /// The create primary key.
    /// </summary>
    internal class CreatePrimaryKey : CreateIndex
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CreatePrimaryKey"/> class.
        /// </summary>
        /// <param name="schema">
        /// The schema.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <param name="primaryKeyName">
        /// The primary key name.
        /// </param>
        /// <param name="dtObjects">
        /// The dt objects.
        /// </param>
        /// <param name="scrOption">
        /// The scr option.
        /// </param>
        public CreatePrimaryKey(string schema, string tableName, string primaryKeyName, DatabaseDataSets dtObjects, SqlScriptingOption scrOption) : base(schema, tableName, primaryKeyName, dtObjects, scrOption)
        {
        }

        /// <summary>
        /// The set rows.
        /// </summary>
        protected override void SetRows()
        {
            this.DataRowViews = this.DatabaseDiffData.GetPrimaryKey(this.Schema, this.TableName);
        }

        /// <summary>
        /// The row script.
        /// </summary>
        protected override void RowScript()
        {
            if (this.IndexRow != null)
            {
                if (!this.IndexRow.NoName)
                {
                    this.AddElement(cmdConstraint, true, true);
                }

                this.AddElement(this.IndexName(), false, true);

                this.AddElement(this.IndexType(), this.IndexRow.NoName, true);
                this.AddElement(this.Clustered(), false, false);
                this.AddRow();
                this.AddRowWithTabAndNewLine("(");
                this.AddElement(Tab + this.Keys(), true, true);
                this.AddRow();
                this.AddElement(")", true, true);
                this.AddElement(this.IndexOption(), false, true);
                this.AddElement(this.FileGroup(), false, true);
                this.AddRowWithComma();
            }
        }

    }
}